package com.yehuishou.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yehuishou.pojo.Address;
import com.yehuishou.pojo.Erwm;
import com.yehuishou.pojo.Identity;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.User;
import com.yehuishou.service.FileService;
import com.yehuishou.service.UserService;
import com.yehuishou.util.MD5Util;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private FileService fileService;

	@RequestMapping("user")
	public String findUser(HttpSession session, Model model) {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "yeah-login";
		}
		
		model.addAttribute("user", user);
		// 查询是否填写地址
		Address addr = userService.findAByUid(user.getUid());
		if (addr != null) {
			model.addAttribute("a", 1);
		}
		
		model.addAttribute("addr", addr);
		// 检查是否审核
		Identity iden = userService.findIById(user.getUid());
		// 是否上传图片
		int k = 0;
		if (iden.getIstate() != 0) {
			String image = iden.getImage();
			if(image!=null) {
				int i = image.length();
				String img = image.replace(";", "");
				int j = img.length();
				if (i - j == 1) {
					// 企业
					k = 1;
				} else {
					// 身份证
					k = 2;
				}
			}
		}
		String erwm = userService.findErwm(user.getUid());
		model.addAttribute("erwm", erwm);
		model.addAttribute("k", k);
		model.addAttribute("istate", iden.getIstate());
		return "info";
	}

	// 修改手机号,密码
	@ResponseBody
	@RequestMapping("updateu")
	public boolean updateu(Integer uid, String phone, String password, String coud, HttpSession session) {
		String coud1 = (String) session.getAttribute("code");
		User user = new User();
		boolean msg;
		if (coud != null) {
			if (!coud1.equals(coud)) {
				// 验证码错误
				msg = false;
			}
			user.setPhone(phone);
		}
		user.setUid(uid);
		user.setPassword(MD5Util.MD5(password));
		user.setUpdated(new Date());
		userService.Update(user);

		User u = (User) session.getAttribute("user");
		u.setPassword(user.getPassword());
		u.setPhone(user.getPhone());
		u.setUpdated(user.getUpdated());
		session.setAttribute("user", u);

		msg = true;
		return msg;
	}

	// 验证手机号是否存在
	@ResponseBody
	@RequestMapping("phone")
	public boolean fphone(String phone) {
		Integer byPhone = userService.findByPhone(phone);
		boolean msg = false;
		if (byPhone == null) {
			msg = true;
		}
		return msg;
	}

	// 保存用户地址
	@RequestMapping("saddress")
	public String saddress(Address address) {
		// 查询地址是否存在
		Address addr = userService.findAByUid(address.getUid());
		if (addr != null) {
			userService.delAddr(address.getUid());
		}
		userService.saddress(address);
		return "redirect:/user";
	}

	// 企业认证
	@RequestMapping("qiye")
	public String qiye(Identity identity, MultipartFile file) throws Exception {
		ImageFile ff = scImage(file);
		int fileId = ff.getFileId();

		identity.setImage(";" + fileId);
		identity.setIstate(1);

		userService.updatei(identity);

		return "redirect:/user";
	}

	// 个人身份
	@RequestMapping("shenfen")
	public String shenfen(Identity identity, MultipartFile filez , MultipartFile fileb ,MultipartFile filex) throws Exception {
		ImageFile fz = scImage(filez);
		int filezId = fz.getFileId();
		
		ImageFile fb = scImage(fileb);
		int filebId = fb.getFileId();
		//判断是否上传学生证
		String filexId ="";
		
		if(filex.getSize() != 0) {
			ImageFile fx = scImage(filex);
			filexId = ";"+fx.getFileId();
		}
		
		identity.setImage(";" + filezId+";" + filebId+filexId);
		identity.setIstate(1);

		userService.updatei(identity);

		return "redirect:/user";
	}

	//二维码
	@RequestMapping("erwm")
	public String erwm(Integer uid, MultipartFile file) throws Exception {
		ImageFile ff = scImage(file);
		Erwm erwm = new Erwm();
		erwm.setUid(uid);
		erwm.setErwm(ff.getFilePath());
		userService.saveErwm(erwm);

		return "redirect:/user";
	}
	
	// 上传图片
	public ImageFile scImage(MultipartFile image) throws IOException {
		String rootPath = "/upload/";
		String model = "public";
		// 获取图片的原始名称
		String ordName = image.getOriginalFilename();
		// 组装文件储存路径
		String filepath = rootPath + model + File.separator;
		// 创建目录
		File f = new File(filepath);
		if (!f.exists()) {
			f.mkdirs();
		}
		// 生成一个新的不会重复的文件名
		// 1.获取后缀
		String suffix = FilenameUtils.getExtension(ordName);
		// 生成新的文件名
		String newName = UUID.randomUUID().toString() + "." + suffix;
		// 把上传的文件储存指定位置
		image.transferTo(new File(f, newName));// 文件上传
		String FilePath = rootPath + model + File.separator + newName;// 文件路径
		ImageFile ff = new ImageFile();
		ff.setFilePath(FilePath.replace("\\", "/"));
		fileService.save(ff);
		return ff;
	}

}
